package logic

import (
	"api/pkg"
	"context"
	"user_srv/user"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateRealNameLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateRealNameLogic {
	return &CreateRealNameLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateRealNameLogic) CreateRealName(req *types.RealNameRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	//userId := l.ctx.Value("userId").(string)
	//uid, _ := strconv.Atoi(userId)
	name, err := l.svcCtx.UserSrv.CreateRealName(l.ctx, &user.CreateRealNameReq{
		RealName: req.RealName,
		IdCard:   req.IdCard,
		UserId:   1,
	})

	if !pkg.RealName(req.RealName, req.IdCard) {
		return &types.Response{
			Code: -1,
			Msg:  "实名认证失败",
			Data: nil,
		}, nil
	}
	if err != nil {
		return &types.Response{
			Code: -2,
			Msg:  "服务器内部错误",
			Data: nil,
		}, nil
	}
	return &types.Response{
		Code: 0,
		Msg:  "实名认证成功",
		Data: map[string]interface{}{
			"name": name,
		},
	}, nil

}
